import {
  LoginModel
} from '../../model/LoginModel.js'

const loginModel = new LoginModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    passError: null,
    userError: null,
    userName: '',
    password: '', //'xum1C)aR4mvl0rXvGf',
    isLoading: false,
    isLogin: false,
    draftInfo: [],
    draftsCount: 0,
    loadingData: false,
    loadingBottom: false,
    more: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    loginModel.validateToken().then(res => {
      this.setData({
        isLogin: true,
        userName: loginModel.getUserName()
      });
      return loginModel.getDrafts(0);
    }).then(res => {
      this.setData({
        draftInfo: res.data.draftInfo,
        draftsCount: res.data.draftsCount
      });
    }).catch(error => {

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  onLogin: function() {
    let userName = this.data.userName;
    let password = this.data.password;
    const uPattern = /^[a-zA-Z0-9_-_.]{4,16}$/;
    const pPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])/;
    if (!uPattern.test(userName)) {
      this.clearInfo(1);
      return;
    }
    if (!pPattern.test(password)) {
      this.clearInfo(2);
      return;
    }
    this.setData({
      isLoading: true
    });
    loginModel.validateToken().then(res => {
      this.loginSuccess();
    }).catch(error => {
      if (error) {
        loginModel.loginByPost(this.data.userName, this.data.password).then(res => {
          if (res) {
            loginModel.setToken(res.data.token);
            loginModel.setUserName(res.data['user_display_name']);
            setTimeout(() => {
              this.loginSuccess(res.data);
            })
            return loginModel.getDrafts(0);
          }
        }).then(res => {
          this.setData({
            draftInfo: res.data.draftInfo,
            draftsCount: res.data.draftsCount
          });
        })
      }
    });

  },
  onUserChange: function(event) {
    if (event.detail) {
      this.setData({
        userName: event.detail
      })
    }
  },
  onPassChange: function(event) {
    if (event.detail) {
      this.setData({
        password: event.detail
      })
    }
  },
  clearInfo: function(type) {
    this.setData({
      userName: '',
      password: '',
      userError: type == 1 ? '用户名不合法' : null,
      passError: type == 2 ? '密码不符合规则，请重新输入' : null
    });
    setTimeout(() => {
      this.setData({
        userError: null,
        passError: null
      })
    }, 1000);
  },
  loginSuccess: function(userData = {}) {
    this.setData({
      isLoading: false,
      isLogin: true,
      userName: loginModel.getUserName()
    });
  },
  _hasMore() {
    return this.data.draftInfo.length < this.data.draftsCount
  },
  _getCurrentOffset(){
    return this.data.draftInfo.length;
  },
  onReachBottom: function() {
    if (!this.data.loadingData) {
      if (this._hasMore()) {
        this.setData({
          loadingBottom: true
        });
        loginModel.getDrafts(this._getCurrentOffset()).then(res => {
          let tempArray = this.data.draftInfo.concat(res.data.draftInfo)
          this.setData({
            draftInfo: tempArray,
            draftsCount: res.data.draftsCount,
            loadingBottom: false
          })
        }, error => {
          this.setData({
            loadingBottom: false
          })
        });
      } else {
        this.setData({
          more: false
        })
      }

    }
  },
})